package com.ruoyi.official.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.check.domain.Overtime;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.personnel.domain.Department;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.mapper.DepartmentMapper;
import com.ruoyi.personnel.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.official.mapper.ReceiveMapper;
import com.ruoyi.official.domain.Receive;
import com.ruoyi.official.service.IReceiveService;

/**
 * 接待管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-06
 */
@Service
public class ReceiveServiceImpl implements IReceiveService 
{
    @Autowired
    private ReceiveMapper receiveMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentMapper departmentMapper;


    /**
     * 查询接待管理
     * 
     * @param id 接待管理主键
     * @return 接待管理
     */
    @Override
    public Receive selectReceiveById(Integer id)
    {
        return receiveMapper.selectReceiveById(id);
    }

    /**
     * 查询接待管理列表
     * 
     * @param receive 接待管理
     * @return 接待管理
     */

    @Override
    public List<Receive> selectReceiveList(Receive receive)
    {
        List<Receive> receives = receiveMapper.selectReceiveList(receive);
        for (Receive receive1 : receives) {
            Department department = departmentMapper.selectDepartmentByDepId(receive1.getDepId());
            receive1.setDepName(department.getDepName());
            Employee employee = employeeMapper.selectEmployeeByEmpId(receive1.getEmpId());
            receive1.setEmpName(employee.getEmpName());
        }
        return receives;
    }

    /**
     * 新增接待管理
     * 
     * @param receive 接待管理
     * @return 结果
     */
    @Override
    public int insertReceive(Receive receive)
    {
        receive.setCreateTime(DateUtils.getNowDate());
        return receiveMapper.insertReceive(receive);
    }

    /**
     * 修改接待管理
     * 
     * @param receive 接待管理
     * @return 结果
     */
    @Override
    public int updateReceive(Receive receive)
    {
        receive.setUpdateTime(DateUtils.getNowDate());
        return receiveMapper.updateReceive(receive);
    }

    /**
     * 批量删除接待管理
     * 
     * @param ids 需要删除的接待管理主键
     * @return 结果
     */
    @Override
    public boolean deleteReceiveByIds(Integer[] ids)
    {
        Receive receive1 = receiveMapper.selectReceiveById(ids[0]);

        Integer audit = receive1.getAudit();
        if (audit==2){
            receiveMapper.deleteReceiveByIds(ids);
            return true;
        }
        String[] arr = new String[ids.length];
        for (int i = 0; i < ids.length; i++) {
            System.out.println("删除的id值--" + ids[i]);
            //根据id查询出时间
            Receive receive = receiveMapper.selectReceiveById(ids[i]);
            //数据库结束时间
            Date otEndTime = receive.getVisitTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String otEndTime01 = simpleDateFormat.format(otEndTime);
            System.out.println("数据库结束时间" + otEndTime);

            //当前时间
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String nowTime = formatter.format(date);
            //与当前时间做比较
            Date otEndTime02 = null;
            Date nowTime02 = null;
            try {
                otEndTime02 = simpleDateFormat.parse(otEndTime01);
                nowTime02 = simpleDateFormat.parse(nowTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            //是否超过
            boolean before = otEndTime02.before(nowTime02);
            if (before == true) {
                //删除
                arr[i] = "0";
            } else {
                arr[i] = "1";
            }
        }
        //数据
        String str = Arrays.toString(arr);
        if (str.contains("1")){
            return false;
        } else {
             receiveMapper.deleteReceiveByIds(ids);
            return true;
        }



        //return receiveMapper.deleteReceiveByIds(ids);
    }

    /**
     * 删除接待管理信息
     * 
     * @param id 接待管理主键
     * @return 结果
     */
    @Override
    public int deleteReceiveById(Integer id)
    {
        return receiveMapper.deleteReceiveById(id);
    }
}
